--[[
@classmod Config
Loads default.cfg
]]
local lume = require (LIB_PATH .. "lume")
local sep = pl.compat.dir_separator

local output_list = function (self, list)
      return lume.reduce(lume.map(list, function (v)
				     return v[1]
				 end), function (v, w) return string.format("%s %s", v, w) end)
 end

return pl.class {
	_name = "Config",
	
	_init = function (self, conf)
		self:load_config(conf or "default")
		self:load_font("Hack-Regular")
	end,
	
	load_config = function (self, f)
		local conf = pl.config
		local save_dir = _lf.getSaveDirectory()
		local path = "default.cfg"
		local fpath = save_dir .. "/" .. path
		local data, err = _lf.read(path)
		assert(data, err)
		_lf.write(path, data)
		local t = conf.read(fpath)
		assert(t, "can't read configuration file " .. fpath)
		self.tbl = t
	end,
	
	load_font = function (self, font_name)
		local path = string.format("assets/fonts/%s.ttf", font_name)
		if _lf.getInfo(path) then
			self.font = _lg.newFont(path, self.tbl.font_size)
			_lg.setFont(self.font)
		else
			return string.format("Font %s not found", path)
		end
	end,
	
	get_font = function (self)
		return self.font
	end,
	
	get_font_list = function (self)
		local list = pl.dir.getFiles('assets/fonts')
		output_list (list)
	end,
	
	dump = function (self)
		pl.pretty.dump(self)
	end,

}
